﻿using System.Linq;
using System.Windows;
using System.Windows.Input;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using Autorent.Desktop.Main;
using Autorent.Desktop.Modules.Autoes.ViewModels;
using Autorent.Desktop.Modules.Clients.ViewModels;
using Autorent.Desktop.Modules.Employees.ViewModels;
using Autorent.Desktop.Modules.Offices.ViewModels;
using Autorent.Desktop.Modules.Orders.ViewModels;
using Autorent.Desktop.Modules.Reports;
using Autorent.Desktop.Modules.Reports.ViewModels;
using Autorent.Desktop.Modules.Reports.OfficesReports.ViewModels;

namespace Autorent.Desktop
{
    public class ShellViewModel
    {
        #region Public Properties

        public Shell View { get; set; }

        #endregion

        #region Constructors

        public ShellViewModel(Shell view, IRegionManager regionManager, IUnityContainer container, IModuleManager moduleManager)
        {
            View = view;
            View.Model = this;

            _container = container;
            _regionManager = regionManager;
            _moduleManager = moduleManager;
        }

        #endregion

        #region Commands

        public ICommand SelectTask
        {
            get
            {
                return new RelayCommand(param => selectTask(param as string));
            }
        }

        #endregion

        #region Protected Methods

        //Clear one of regions in view
        protected void ClearRegion(IRegion region)
        {
            if (region != null)
            {
                foreach (object view in region.ActiveViews)
                {
                    region.Remove(view);
                }
            }
        }

        //Clear regions which displayed by application task selection
        protected void ClearDetailRegions()
        {
            foreach (IRegion region in _regionManager.Regions)
            {
                foreach (object view in region.ActiveViews)
                {
                    region.Remove(view);
                }
            }
        }

        #endregion

        #region Private Variables

        private readonly IUnityContainer _container;
        private readonly IRegionManager _regionManager;
        private readonly IModuleManager _moduleManager;

        #endregion

        #region Private Methods

        //Activate selected application task view
        private void selectTask(string item)
        {
            IRegion listRegion = null;
            IRegion applicationContentRegion = null;
            IRegion detailsRegion = null;
            IRegion searchRegion = null;

            if (_regionManager.Regions.Count() != 0)
            {
                listRegion = _regionManager.Regions["ListRegion"];
                applicationContentRegion = _regionManager.Regions["ApplicationContentRegion"];
                detailsRegion = _regionManager.Regions["DetailsRegion"];
                searchRegion = _regionManager.Regions["SearchRegion"];
            }

            switch (item)
            {
                case "myOrders":
                    if (listRegion != null)
                    {
                        ClearDetailRegions();

                        if (_regionManager.Regions["ListRegion"].GetView("MyOrdersListView") == null)
                        {
                            _regionManager.Regions["ListRegion"].Add(
                                _container.Resolve<OrdersListViewModel>(new ParameterOverrides() { { "filterType", OrdersListViewModel.OrdersFilterType.ByManager } }).View, "MyOrdersListView");
                        }
                    }
                    break;
                case "newOrders":
                    ClearDetailRegions();

                    if (_regionManager.Regions["ListRegion"].GetView("NewOrdersListView") == null)
                    {
                        _regionManager.Regions["ListRegion"].Add(
                        _container.Resolve<OrdersListViewModel>(new ParameterOverrides() { { "filterType", OrdersListViewModel.OrdersFilterType.New } }).View, "NewOrdersListView"); 
                    }
                    break;
                case "allOrders":
                    ClearDetailRegions();

                    if (_regionManager.Regions["ListRegion"].GetView("AllOrdersListView") == null)
                    {
                        _regionManager.Regions["ListRegion"].Add(
                        _container.Resolve<OrdersListViewModel>(new ParameterOverrides() { { "filterType", OrdersListViewModel.OrdersFilterType.All } }).View, "AllOrdersListView");
                    }
                    break;
                case "clients":
                    ClearDetailRegions();
                    
                    if (_regionManager.Regions["ListRegion"].GetView("ClientsListView") == null) { _regionManager.Regions["ListRegion"].Add(_container.Resolve<ClientsListViewModel>().View, "ClientsListView"); }
                    break;
                case "cars":
                    ClearDetailRegions();

                    if (_regionManager.Regions["ListRegion"].GetView("AutoesListView") == null) { _regionManager.Regions["ListRegion"].Add(_container.Resolve<AutoesListViewModel>().View, "AutoesListView"); }
                    break;
                case "employees":
                    ClearDetailRegions();

                    if (_regionManager.Regions["ListRegion"].GetView("EmployeesListView") == null) { _regionManager.Regions["ListRegion"].Add(_container.Resolve<EmployeesListViewModel>().View, "EmployeesListView"); }
                    break;
                case "offices":
                    ClearDetailRegions();

                    if (_regionManager.Regions["ListRegion"].GetView("OfficesListView") == null) { _regionManager.Regions["ListRegion"].Add(_container.Resolve<OfficesListViewModel>().View, "OfficesListView"); }
                    break;
                case "reports":
                    ClearDetailRegions();

                    if (_regionManager.Regions["ApplicationContentRegion"].GetView("ReportView") == null) { _regionManager.Regions["ApplicationContentRegion"].Add(_container.Resolve<MainReportsViewModel>().View, "MainReportsView"); }
                    break;
            }
        }

        #endregion
    }
}
